package com.bdy.lm.taximanager.thread;

import android.util.Log;

import com.bdy.lm.taximanager.app.MyApp;
import com.bdy.lm.taximanager.http.HttpJson;
import com.bdy.lm.taximanager.model.ServerCommandEvent;
import com.bdy.lm.taximanager.util.TimeUtils;
import com.bdy.lm.taximanager.model.weather.Result;
import com.bdy.lm.taximanager.model.weather.Weather;
import com.google.gson.Gson;

import de.greenrobot.event.EventBus;

/**
 * Created by Administrator on 2016/11/29 0029.
 */

public class WeatherThread extends Thread {

    private HttpJson httpJson = new HttpJson();
    private String flag = "天气更新线程";
    private long updateTime = 0;
    private ServerCommandEvent serverCommandEvent = new ServerCommandEvent();
    private boolean begin = true;

    public void run() {
        while (MyApp.threadRun) {
            delay(100000);
            if(TimeUtils.getStampTime() - updateTime > 600000){
                begin = true;
                while (begin) {
                    httpJson.HttpJsonGet(MyApp.askUrl);
                    if(httpJson.getGetJson() != null){
                        Weather weather = new Gson().fromJson(httpJson.getGetJson().toString(), Weather.class);
                        Result result = weather.getResult();
                        if (result == null) {
                            continue;
                        }
                        String weaInfo = getWeatherMsg(result);
                        serverCommandEvent.setReceivedMessage(weaInfo);
                        EventBus.getDefault().post(serverCommandEvent);
                        updateTime = TimeUtils.getStampTime();
                        begin = false;
                        Log.d("Weather",weaInfo);
                    } else {
                        delay(1500);
                    }
                }
            }
        }
    }
    private String getWeatherMsg(Result result){
        StringBuffer weatherInfo = new StringBuffer();
        weatherInfo.append("实时天气  日期：");
        weatherInfo.append(result.getDays());
        weatherInfo.append(" ");
        weatherInfo.append(result.getWeek());
        weatherInfo.append(", 城市：");
        weatherInfo.append(result.getCitynm());
        weatherInfo.append(", 实时温度：");
        weatherInfo.append(result.getTemperature_curr());
        weatherInfo.append("  今日气温：");
        weatherInfo.append(result.getTemperature());
        weatherInfo.append("  湿度：");
        weatherInfo.append(result.getHumidity());
        weatherInfo.append("  天气:");
        weatherInfo.append(result.getWeather());
        weatherInfo.append(", 风向：");
        weatherInfo.append(result.getWind());
        weatherInfo.append(", 风力：");
        weatherInfo.append(result.getWinp());
        weatherInfo.append(", 最高温度：");
        weatherInfo.append(result.getTemp_high());
        weatherInfo.append(", 最低温度：");
        weatherInfo.append(result.getTemp_low());
        weatherInfo.append(", 实时湿度：");
        weatherInfo.append(result.getTemp_curr());
        return weatherInfo.toString();
    }
    void delay(int time) {
        try {
            Thread.sleep(time);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
